package com.pig.graduate.service.impl.sys;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pig.graduate.domain.pigFarm.dto.BuildFarmDTO;
import com.pig.graduate.domain.pigFarm.BuildsFarm;
import com.pig.graduate.domain.pigFarm.dto.IndexStockDTO;
import com.pig.graduate.domain.sys.SysBuildings;
import com.pig.graduate.mapper.produce.ProducePigMapper;
import com.pig.graduate.result.MyResult;
import com.pig.graduate.service.sys.SysBuildingsService;
import com.pig.graduate.mapper.sys.SysBuildingsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 星星眼
* @description 针对表【sys_buildings】的数据库操作Service实现
* @createDate 2023-02-19 17:22:43
*/
@Service
public class SysBuildingsServiceImpl extends ServiceImpl<SysBuildingsMapper, SysBuildings>
    implements SysBuildingsService{

    @Autowired
    private SysBuildingsMapper sysBuildingsMapper;

    @Autowired
    private ProducePigMapper producePigMapper;

    @Override
    public MyResult getBuildsFarm(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<BuildFarmDTO> boarList = sysBuildingsMapper.getBuildsFarmForBoar();
        Map<Long,BuildsFarm> map = new HashMap<>();
        for (BuildFarmDTO buildFarmDTO : boarList) {
            if(map.containsKey(buildFarmDTO.getId())){
                if(buildFarmDTO.getPigType().equals(0)){
                    map.get(buildFarmDTO.getId()).setBoar(buildFarmDTO.getNumber());
                }else{
                    map.get(buildFarmDTO.getId()).setSow(buildFarmDTO.getNumber());
                }
                map.get(buildFarmDTO.getId()).setTotal(map.get(buildFarmDTO.getId()).getTotal() + buildFarmDTO.getNumber());
            }else{
                BuildsFarm buildsFarm = new BuildsFarm();
                if(buildFarmDTO.getPigType().equals(0)){
                    buildsFarm.setBoar(buildFarmDTO.getNumber());
                }else{
                    buildsFarm.setSow(buildFarmDTO.getNumber());
                }
                buildsFarm.setColumnName(buildFarmDTO.getName());
                buildsFarm.setTotal(buildFarmDTO.getNumber());
                buildsFarm.setMaxColumnNumber(buildFarmDTO.getMaxNumber());
                map.put(buildFarmDTO.getId(), buildsFarm);
            }
        }
        List<BuildFarmDTO> pigList = sysBuildingsMapper.getBuildsFarmForPig();
        for (BuildFarmDTO buildFarmDTO : pigList) {
            if(map.containsKey(buildFarmDTO.getId())){
                map.get(buildFarmDTO.getId()).setPig(buildFarmDTO.getNumber());
                map.get(buildFarmDTO.getId()).setTotal(map.get(buildFarmDTO.getId()).getTotal() + buildFarmDTO.getNumber());
            }else{
                BuildsFarm buildsFarm = new BuildsFarm();
                buildsFarm.setColumnName(buildFarmDTO.getName());
                buildsFarm.setTotal(buildFarmDTO.getNumber());
                buildsFarm.setMaxColumnNumber(buildFarmDTO.getMaxNumber());
                map.put(buildFarmDTO.getId(), buildsFarm);
            }
        }
        map.values().forEach(build->{
            Double total = (double)build.getTotal();
            Double maxNumber = (double)build.getMaxColumnNumber();
            double temp = total / maxNumber * 100;
            build.setUsage(String.format("%.2f", temp)+"%");
        });
        PageInfo<BuildsFarm> pageInfo = new PageInfo(new ArrayList<>(map.values()));
        return MyResult.success(pageInfo);
    }


    public MyResult getIndexStock(){
        IndexStockDTO indexStock = sysBuildingsMapper.getIndexStock();
        return MyResult.success(indexStock);
    }
}




